/*
 * Tools.h
 *
 *  Created on: 10 oct. 2012
 *      Author: raphael.capocasa
 */

#ifndef TOOLS_H_
#define TOOLS_H_

#include <list>
#include <string>
#include <sstream>
#include <cctype>
#include <iostream>
#include "Object.h"
#include "WorkspaceTools.h"

namespace raph
{
namespace tools
{
class Convert
    {
    public:
        static type::T_UINT16 charToUint16(const char&);
        static std::list<type::T_UINT16>* stringNumberToList(const std::string& str);
        /**
         * @brief this function return a string representation of number
         * @param numeric
         * @return
         */
        template<typename U> static std::string numericToString(U& numeric);
        template<typename U> static U stringToNumeric(const std::string& str);
        template<typename U> static std::string toString(const U &data);
    }
;
class Memory
    {
    public:
        template<typename T> static void deallocate(T** var);
        template<typename T> static void deallocateList(std::list<T>** container);
        template<typename T> static void deallocateTab(T** tab);
    };

class Copy
    {
    public:
        template<typename T> static std::list<T>* copyList(std::list<T>* source);
    };

class Numbers
    {
    public:
        /**
         * @brief Check the equality between two floating numbers. The error marge is a percentage about the first number
         * @param n1
         * @param n2
         * @param percentage
         * @return
         */
        static bool isEqual(const type::T_FLOAT128& n1, const type::T_FLOAT128& n2,
                const type::T_FLOAT128& percentage) noexcept;

        static bool isPrime(const unsigned int &n);
    };

/*-------------------------------------------------------------------
 CONVERT
 -------------------------------------------------------------------*/
template<typename U>
std::string Convert::numericToString(U& numeric)
    {
    std::ostringstream oss;
    oss << numeric;
    return oss.str();
    }

template<typename U> U Convert::stringToNumeric(const std::string& str)
    {
    std::istringstream iss(str, std::istringstream::in);
    U numeric;
    iss >> numeric;
    return numeric;
    }

template<typename U> std::string Convert::toString(const U &data)
    {
    std::ostringstream oss;
    oss << data;
    return oss.str();
    }

/*-------------------------------------------------------------------
 MEMORY
 -------------------------------------------------------------------*/
template<typename T> void Memory::deallocate(T** var)
    {
    delete *var;
    *var = 0;
    }

template<typename T> void Memory::deallocateList(std::list<T>** container)
    {
    while (!(*container)->empty())
        {
        (*container)->pop_back();
        }
    deallocate(container);
    }

template<typename T> void Memory::deallocateTab(T** tab)
    {
    delete[] *tab;
    *tab = 0;
    }

template<typename T> std::list<T>* Copy::copyList(std::list<T>* source)
    {
    std::list<T>* target = new std::list<T>();
    for (T &elem : *source)
        {
        target->push_back(elem);
        }
    return target;
    }
}
}
#endif /* TOOLS_H_ */
